<!DOCTYPE html>
<!--
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHPWebPage.php to edit this template
-->
<?php
    $frist=!count($_POST);
    session_start();
    if(!$_SESSION['islogin']){
         echo "<script>alert('请先进行登录');window.location.href='login.php'</script>";
    }
    $username = $_SESSION['username'];
    require 'common/common.php';
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
//            alert(editt.value);
            window.onload=function(){
            //左侧点击切换右侧
                var lis = document.querySelectorAll(".left_box li");
                var items = document.querySelectorAll(".right_box div");
                for(var i =0;i<lis.length;i++){
                    lis[i].index = i;
                    lis[i].addEventListener('click',function(){
                         for(var j =0;j<items.length;j++){
                            lis[j].className="";
                            items[j].style.display="none";
                        }
                        this.className="selected";
                        items[this.index].style.display="block";
                    });
                }
                //左侧tab下拉
                
                for(var j=1;j<2;j++){
                 lis[0].onclick=function(){
                    if(lis[1].style.display==="none"){
                        lis[0].innerHTML="用户管理 &#8594";
                         for(var i =1;i<4;i++){
                            lis[i].style.display="block";
                         }
                    }else{
                         lis[0].innerHTML="用户管理 &#8595";
                         for(var i =1;i<2;i++){
                            lis[i].style.display="none";
                         }
                    }
                };
               }
               for(var j=3;j<6;j++){
                    lis[2].addEventListener('click',function(){
                        if(lis[3].style.display==="none"){
                           lis[2].innerHTML="文章管理 &#8594";
                            for(var i =3;i<6;i++){
                               lis[i].style.display="block";
                            }
                       }else{
                            lis[2].innerHTML="文章管理 &#8595";
                            for(var i =3;i<6;i++){
                               lis[i].style.display="none";
                            }
                       }
                    });     
                };
            
            };
        </script>
        <style>
            *{
                margin:0;
                padding:0;
                list-style:none;
                text-decoration: none;
            }
            input:focus {
                outline:none;
            }
            input{
                border:none;
                outline:none;
            }
            textarea{
                border:none;
                outline:none;
            }
            textarea:focus {
                outline:none;
            }
            .top_box{
               background-color:#2F4F4F;
               width:100%;
               height:100px;
            }
            .top_box h2{
                float:left;
                line-height: 100px;
                margin-left: 10px;
            }
            .top_right{
                float:right;
                margin:26px 10px 0 0;
            }
            .top_right a{
                color:black;
            }
            .left_box{
                float:left;
                width:200px;
                
            }
            .left_box ul{
                height:1000px;
                width:200px;
                background-color:#EAEAEF;
            }
            .left_box li{
                width:200px;
                height:60px;
                text-align: center;
                line-height: 60px;
                font-size:19px;
            }
            .left_box li:hover{
                background-color:#808080;
                color:white;
                cursor:pointer;
            }
            
            .first-contnet{
                display: block;
            }
            .selected{
                background-color:#808080;
                color:white;
            }
            .top_right>h3>span {
                color:#CD7F32;
            }
            .top_right>span{
                display: inline-block;
                margin-bottom: 20px;
            }
            .top_right>a{
                display: inline-block;
                color:#FF7F00;
                margin-top:10px;
            }
            .right_box{
                width: 100%;
                height: 1000px;
                background-color: buttonface;
            }
            .fourth-contnet,
            .second-contnet,
            .third-contnet{
                display: none;
            }
            .first_contnet,
            .third-contnet{
                position:relative;
                float:left;
                margin:150px 0 0 200px;
                width: 1200px;
                height: 500px;
                border:1px solid skyblue;
                background-color: #DCDCDC;

            }
            .first_contnet span,
            .third-contnet span{
                display: inline-block;
            }

            .edit{
                cursor:pointer;
            }
            #close{
                cursor:pointer;
            }
            .fourth-contnet{
                margin:100px 0 0 627px;
            }
            .fourth-contnet input{
                height: 30px !important;
                line-height:30px;
            }
            .fourth-contnet input, 
            .fourth-contnet textarea{
                width: 800px;
                height: 500px; 
                padding: 0px 15px; 
                margin-bottom: 18px; 
                border: 1px #33d182 solid;
                border-radius: 3px;
                font-size: 14px; 
                color: #7093DB; 
                line-height: 30px; 
                background: rgba(51, 209, 130, 0.1); 
                resize:none;
            }
            .fourth-contnet input[type='submit']{
                width: 832px;
                padding: 0px 15px; 
                margin-bottom: 18px; 
                cursor: pointer; 
                background: #33d182;
                font-size: 16px;
                letter-spacing: 2px; 
                border: none;
                color: #fff;
                line-height: 30px;
                margin-right: 15px;
            }
            .b1{
                width: 200px;
                overflow:hidden; 
                text-overflow: ellipsis;  	
                white-space: nowrap;	
            }
        </style>
    </head>
    <body>
         <!--遮罩层-->
    <div id="cover" style="background: #000; position: absolute; left: 0px; top: 0px; width: 100%; height:100%;  display:none; filter: alpha(opacity=30); opacity: 0.3;  z-index: 2 "></div>
        <div class="box">
            <div class="top_box">
                <h2>用户后台管理系统</h2>
                <div class="top_right">
                    <h3> 用户名：<span><?php echo "$username" ?></span></h3>
                        <?php
                        echo "<a  href='quit.php?name=quit'>退出登录</a>"; 
                        ?>
                </div>
            </div>
        
            <div class="left_box ">
                <ul class="list">
                    <li class="text selected">用户管理 &#8594</li>
                    <li class="text">&nbsp;&nbsp;&nbsp;&nbsp;用户创建</li>
                    <li class="text">文章管理 &#8594</li>
                    <li class="text">&nbsp;&nbsp;&nbsp;&nbsp;文章评论管理</li>
                    <li class="text">&nbsp;&nbsp;&nbsp;&nbsp;文章添加</li>
                    <li class="text">&nbsp;&nbsp;&nbsp;&nbsp;文章修改</li>
                </ul>
            </div>
             <div class="right_box">
                    <div class="first_contnet">
                        <table>
                            <thead>
                                <tr>
                                    <th><span style="width:160px">ID</span></th>    
                                    <th><span style="width:160px">用户名</span></th>  
                                    <th><span style="width:240px">用户邮箱</span></th>  
                                    <th><span style="width:240px">用户手机号</span></th>
                                    <th><span style="width:240px" >操作</th>
                                </tr>
                            </thead>
                            
                            <?php 
                                $totalcount = count(getDataSetBySQL("select * from user order by uid desc"));
                                $sql = "select * from user order by uid asc";
                            ?>
                            <tbody>
                            <?php 
                            $a=array();
                                for($index=0;$index<$totalcount;$index++){
                                    ?>
                                    <tr>
                                    <th><span style="width:160px; margin:10px 0;"><?php echo getDataSetBySQL($sql)[$index]["uid"]?></span></th>    
                                    <th><span style="width:160px"><?php echo getDataSetBySQL($sql)[$index]["username"]?></span></th>  
                                    <th><span style="width:240px"><?php echo getDataSetBySQL($sql)[$index]["email"]?></span></th>  
                                    <th><span style="width:240px"><?php echo getDataSetBySQL($sql)[$index]["phone"]?></span></th>
                                    <th>
                                        <span style="width:50px" class="edit"><a href="admin_updata.php?id=<?php echo getDataSetBySQL($sql)[$index]["uid"]?>">编辑</a></span>
                                        <span style="width:50px" class="delete"><a href="admin_delete.php?id=<?php echo getDataSetBySQL($sql)[$index]["uid"]?>">删除</a></span> 
                                    </th>
                                </tr>
                                <?php
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                 <div class="second-contnet ">
                    
                     <img src="<?php getDataSetBySQL($sql)[0]["pic"] ?>" width=200px>
                     <form action="admin.php" enctype="multipart/form-data" method="post">
                         <span>用户名：<input type="text" name="uname"></span></br>
                         <span>密&nbsp;&nbsp; 码：<input type="text" name="passwd"></span></br>
                         <span>邮 &nbsp;&nbsp;箱：<input type="text" name="email"></span></br>
                         <span>手机号：<input type="text" name="phone"></span></br>
                         <span>上传头像：<input type="file" name="ufile"></span></br>
                         <span><input type="submit" name="submit"></span></br>
                     </form> 
                 </div>
                 <div class="third-contnet">
                        <table>
                            <thead>
                                <tr>
                                    <th><span style="width:160px">ID</span></th>    
                                    <th><span style="width:160px">用户名</span></th>  
                                    <th><span style="width:160px">标题</span></th>  
                                    <th><span style="width:360px">内容</th>
                                    <th><span style="width:160px" >操作</th>
                                </tr>
                            </thead>
                            
                            <?php 
                                $totalcount = count(getDataSetBySQL("select * from essay order by essay_id desc"));
                                $sql = "select * from essay order by essay_id asc";
                            ?>
                            <tbody>
                            <?php 
                                for($index=0;$index<$totalcount;$index++){
                                    ?>
                                    <tr>
                                    <th><span style="width:160px; margin:10px 0;"><?php echo getDataSetBySQL($sql)[$index]["essay_id"]?></span></th>    
                                    <th><span style="width:160px"><?php echo getDataSetBySQL($sql)[$index]["user"]?></span></th>  
                                    <th><span style="width:240px"><?php echo getDataSetBySQL($sql)[$index]["essay_name"]?></span></th>
                                    <th><span style="width:240px" class="b1"><?php echo getDataSetBySQL($sql)[$index]["detail"]?></span></th>  
                                    <th>
                                        <span style="width:50px" class="edit"><a href="admin_essay_updata.php?id=<?php echo getDataSetBySQL($sql)[$index]["essay_id"]?>">编辑</a></span>
                                        <span style="width:50px" class="delete"><a href="admin_essay_delete.php?id=<?php echo getDataSetBySQL($sql)[$index]["essay_id"]?>">删除</a></span> 
                                    </th>
                                </tr>
                                <?php
                                }
                                ?>
                            </tbody>
                        </table>
                 </div>
                 <div class="fourth-contnet">文章评论管理</div>
                 <div class="fourth-contnet">
                    <form action="admin.php" enctype="multipart/form-data" method="post">
                        <input type="text" placeholder="输入标题" name="title"/> </br>
                        <textarea  name="content" placeholder="输入10-300内容" maxlength="300" minlength="10"></textarea>
                        <input type="submit" value="提交" name="submit2">
                    </form>
                 </div>
                 <div class="fourth-contnet">文章修改</div>
             </div>
        </div>
         <?php
         if(isset($_POST["submit"])){
                          header("content-type:html;charset=utf-8");
                          $hostname="127.0.0.1";
                          $username="root";
                          $password="123456";
                          $database="add";
                          $link=mysqli_connect($hostname, $username,
                            $password, $database);
                         $uname = $_POST["uname"];
                         $passwd = $_POST["passwd"];
                         $email = $_POST["email"];
                         $phone = $_POST["phone"];
                         $sql = "select *from user where username='$uname'";
                         $res = mysqli_query($link,$sql);
                         $arr = mysqli_fetch_assoc($res);
                        if($arr){
                            echo "<script>alert('用户名已存在')</script>";
                        }else{
                          $sql="INSERT INTO `add`.`user` (`username`, `passwd`, `phone`, `email`)"
                                . " VALUES ('${uname}', '${password}', '${phone}', '${email}');";
                          $res = mysqli_query($link,$sql);
                          $src='images/';
                          $ufile=$_FILES['ufile'];
                          move_uploaded_file($_FILES["ufile"]["tmp_name"], $src.time().$username.$_FILES['ufile']['name']);
                          echo "<img src=".$src.time().$username.$_FILES['ufile']['name']." width=260px  >";
                            if($res){
                                echo "<script>alert('添加成功');window.location.href='admin.php'</script>";
                            }else{
                                echo "<script>alert('添加失败');</script>";
                            }
                        }
                                    
                                     }
        if(isset($_POST["submit2"])){
            header("content-type:html;charset=utf-8");
            $hostname="127.0.0.1";
            $username="root";
            $password="123456";
            $database="add";
            $link=mysqli_connect($hostname, $username,
              $password, $database);
            $uid = $_SESSION['uid'];
            $username= $_SESSION['username'];
            $title = $_POST["title"];
            $content = $_POST["content"];
            $currenttime = date('Y-m-d');
            echo  "$currenttime" ;
            $sql="INSERT INTO essay (`user_id`,`essay_name`,`detail`,`create_time`,`user`) VALUES ('$uid','$title','$content','$currenttime','$username');";
            
            $res = mysqli_query($link,$sql);
            if($res){
                echo "<script>alert('添加成功');window.location.href='admin.php'</script>";
            }else{
                echo "<script>alert('添加失败');</script>";
            }
        }
        ?>
    </body>
</html>
<?php
error_reporting(0);   //防止出现Notice报错，阻断网页运行
session_start();      
if( "$_GET[name]"=="quit"){        //使用GET获取name，判断是否满足条件
    $_SESSION['islogin'] = '';      //重置session
    header('location:admin.php');     //跳转到主页面
}
?>